package co.yixiang.yshop.module.order.controller.admin.storeorderrule;

import co.yixiang.yshop.framework.common.pojo.CommonResult;
import co.yixiang.yshop.module.order.controller.admin.storeorderrule.param.OrderRuleCreateParam;
import co.yixiang.yshop.module.order.controller.admin.storeorderrule.param.OrderRuleUpdateParam;
import co.yixiang.yshop.module.order.controller.admin.storeorderrule.vo.StoreOrderRuleVO;
import co.yixiang.yshop.module.order.service.storeorderrule.StoreOrderRuleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 订单规则")
@RestController
@RequestMapping("/order/rule")
@Validated
public class StoreOrderRuleController {

    @Resource
    private StoreOrderRuleService orderRuleService;

    @PostMapping("/create")
    @Operation(summary = "创建订单规则")
    @PreAuthorize("@ss.hasPermission('order:rule:create')")
    public CommonResult<Integer> create(@Valid @RequestBody OrderRuleCreateParam vo) {
        return success(orderRuleService.create(vo));
    }

    @PutMapping("/update")
    @Operation(summary = "修改订单规则")
    @PreAuthorize("@ss.hasPermission('order:rule:update')")
    public CommonResult<Integer> update(@Valid @RequestBody OrderRuleUpdateParam vo) {
        if(vo.getId() == null){
            return success(orderRuleService.create(vo));
        }
        return success(orderRuleService.update(vo));
    }

    @GetMapping("/get")
    @Operation(summary = "查看订单规则")
    @PreAuthorize("@ss.hasPermission('order:rule:get')")
    public CommonResult<StoreOrderRuleVO> get() {
        return success(orderRuleService.getOrderRuleCache());
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除订单规则")
    @PreAuthorize("@ss.hasPermission('order:rule:delete')")
    public CommonResult<Integer> delete() {
        return success(orderRuleService.delete());
    }

}
